package org.ehe.business.step.controller;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.ehe.business.step.domain.BO.StepAnalysisBO;
import org.ehe.business.step.domain.DTO.StepAnalysisDTO;
import org.ehe.business.step.domain.DTO.StepAnalysisQueryDTO;
import org.ehe.business.step.domain.VO.StepAnalysisVO;
import org.ehe.business.step.service.StepAnalysisService;
import org.ehe.common.core.domain.R;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * STEP文件分析结果管理
 * @author :zhangnn
 * @className :StepAnalysisController
 * @description: TODO
 * @date 2025-08-06 14:00:37
 */
@Slf4j
@RestController
@RequestMapping("/step-analysis")
@Validated
@CrossOrigin(origins = "*")
public class StepAnalysisController {
    @Autowired
    private StepAnalysisService stepAnalysisService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public TableDataInfo<StepAnalysisVO> getPage(StepAnalysisBO bo, PageQuery query) {
        return stepAnalysisService.getPage(bo,query);
    }

    /**
     * 查询所有记录
     */
    @GetMapping("/all")
    public R<List<StepAnalysisDTO>> getAll() {
        try {
            List<StepAnalysisDTO> result = stepAnalysisService.findAll();
            return R.ok(result);
        } catch (Exception e) {
            log.error("查询所有STEP分析结果失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 创建分析结果
     */
    @PostMapping
    public R<StepAnalysisDTO> create(@Valid @RequestBody StepAnalysisDTO dto) {
        try {
            log.info("创建STEP分析结果请求: {}", dto.getFileName());
            StepAnalysisDTO result = stepAnalysisService.create(dto);
            return R.ok("创建成功");
        } catch (Exception e) {
            log.error("创建STEP分析结果失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 根据ID查询详情
     */
    @GetMapping("/{id}")
    public R<StepAnalysisDTO> getById(@PathVariable @NotNull Long id) {
        try {
            StepAnalysisDTO result = stepAnalysisService.findById(id);
            return R.ok(result);
        } catch (Exception e) {
            log.error("查询STEP分析结果失败，ID: " + id, e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 根据文件名查询
     */
    @GetMapping("/file/{fileName}")
    public R<StepAnalysisDTO> getByFileName(@PathVariable String fileName) {
        try {
            StepAnalysisDTO result = stepAnalysisService.findByFileName(fileName);
            return R.ok(result);
        } catch (Exception e) {
            log.error("根据文件名查询STEP分析结果失败: " + fileName, e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 根据请求ID查询
     */
    @GetMapping("/request/{requestId}")
    public R<StepAnalysisDTO> getByRequestId(@PathVariable String requestId) {
        try {
            StepAnalysisDTO result = stepAnalysisService.findByRequestId(requestId);
            return R.ok(result);
        } catch (Exception e) {
            log.error("根据请求ID查询STEP分析结果失败: " + requestId, e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 更新分析结果
     */
    @PutMapping("/{id}")
    public R<StepAnalysisDTO> update(
        @PathVariable @NotNull Long id,
        @Valid @RequestBody StepAnalysisDTO dto) {
        try {
            log.info("更新STEP分析结果请求，ID: {}", id);
            StepAnalysisDTO result = stepAnalysisService.updateInfo(id, dto);
            return R.ok(result);
        } catch (Exception e) {
            log.error("更新STEP分析结果失败，ID: " + id, e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除分析结果
     */
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable @NotNull Long id) {
        try {
            log.info("删除STEP分析结果请求，ID: {}", id);
            stepAnalysisService.delete(id);
            return R.ok(null);
        } catch (Exception e) {
            log.error("删除STEP分析结果失败，ID: " + id, e);
            return R.fail(e.getMessage());
        }
    }



    /**
     * 批量删除
     */
    @DeleteMapping("/batch")
    public R<Void> batchDelete(@RequestBody List<Long> ids) {
        try {
            log.info("批量删除STEP分析结果，数量: {}", ids.size());
            for (Long id : ids) {
                stepAnalysisService.delete(id);
            }
            return R.ok(null);
        } catch (Exception e) {
            log.error("批量删除STEP分析结果失败", e);
            return R.fail(e.getMessage());
        }
    }

    /**
     * 导出数据（返回所有数据用于导出）
     */
//    @GetMapping("/export")
//    public R<List<StepAnalysisDTO>> export(StepAnalysisQueryDTO queryDTO) {
//        try {
//            log.info("导出STEP分析结果数据");
//            // 设置一个很大的分页参数来获取所有数据
//            queryDTO.setPage(1);
//            queryDTO.setSize(10000);
//            PageResult<StepAnalysisDTO> result = stepAnalysisService.findByPage(queryDTO);
//            return R.ok(result.getRecords(), result.getTotal());
//        } catch (Exception e) {
//            log.error("导出STEP分析结果失败", e);
//            return R.fail(e.getMessage());
//        }
//    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    public R<String> health() {
        return R.ok("STEP Analysis API is running");
    }
}
